package com.ahao.aggregation;

import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import java.util.Timer;
import java.util.TimerTask;

public class ZHFloatView {
    private ViewGroup parentView;
    private View item;
    private FrameLayout.LayoutParams layoutParams;
    private View.OnClickListener itemOnclickListener;

    // 浮动按钮按下时x坐标
    final float[] downX = new float[1];
    // 浮动按钮按下时y坐标
    final float[] downY = new float[1];
    // 按下时浮层x坐标
    final float[] downViewX = {0};
    private Timer timer;

    public ZHFloatView(FrameLayout.LayoutParams layoutParams) {
        this.layoutParams = layoutParams;
    }

    public ViewGroup getParentView() {
        return parentView;
    }

    public void setParentView(ViewGroup parentView) {
        this.parentView = parentView;
    }

    public View getItem() {
        return item;
    }

    public void setItem(View item) {
        this.item = item;
    }

    public void hideItem() {
        item.setVisibility(View.GONE);
    }

    public void showItem() {
        item.setVisibility(View.VISIBLE);
    }

    public FrameLayout.LayoutParams getLayoutParams() {
        return layoutParams;
    }

    /**
     * 初始化浮动事件
     *
     * @param width  外容器宽度
     * @param height 外容器高度
     */
    public void initFloatEvent(final int width, final int height) {
        item.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                item.setAlpha(1f);
                // 返回false，是点击事件；返回true，不记为点击事件 参考链接  https://blog.csdn.net/u014043113/article/details/74778414
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        // 按下
                        // 恢复动画
                        if (timer != null) {
                            timer.cancel();
                        }
                        downX[0] = event.getX();
                        downY[0] = event.getY();
                        downViewX[0] = item.getX();
                        return true;
                    case MotionEvent.ACTION_MOVE:
                        // 移动
                        // 移动的距离
                        float moveX = event.getX() - downX[0]; // event.getX() 移动的X距离
                        float moveY = event.getY() - downY[0]; // event.getY() 移动的Y距离
                        // 当前view= X,Y坐标
                        float viewX = item.getX();
                        float viewY = item.getY();
                        // view的宽高
                        int viewHeigth = item.getWidth();
                        int viewWidth = item.getHeight();

                        // X当超出屏幕,取最大值
                        if (viewX + moveX + viewWidth > width) {
                            // 靠右
                            item.setX(width - viewWidth);
                        } else if (viewX + moveX <= 0) {
                            // 靠右
                            item.setX(0);
                        } else {
                            // 正常
                            item.setX(viewX + moveX);
                        }
                        // Y当超出屏幕,取最大值
                        if (viewY + moveY + viewHeigth > height) {
                            //靠下
                            item.setY(height - viewHeigth);
                        } else if (viewY + moveY <= 0) {
                            //靠上
                            item.setY(0);
                        } else {
                            // 正常
                            item.setY(viewY + moveY);
                        }
                        return true;


                    case MotionEvent.ACTION_UP:
                        // 松手
                        float upX = item.getX();
                        // 屏幕中心点
                        float center = width / 2;
                        if (item.getX() > center) {
                            // 靠右
                            item.animate().x(width - item.getWidth()).setDuration(300).start();
                        } else {
                            item.animate().x(0).setDuration(300).start();
                        }
                        shrinkAnim(item.getX() > center);
                        // 按下时与松手时X值一致的话，就干点别的事情
                        if (downViewX[0] == upX) {
                            if (itemOnclickListener != null) {
                                itemOnclickListener.onClick(v);
                            }
                            return false;
                        } else {
                            return true;
                        }
                }
                return false;
            }
        });
        // 启动收缩动画
        shrinkAnim(false);
    }

    /**
     * 收缩图标
     *
     * @param isRight 是否停靠在右边
     */
    private void shrinkAnim(final boolean isRight) {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                item.post(new Runnable() {
                    @Override
                    public void run() {
                        if (isRight) {
                            item.animate().x(item.getX() + item.getWidth() / 2).alpha(0.8f).setDuration(1200).start();
                        } else {
                            item.animate().x(0 - item.getWidth() / 2).alpha(0.8f).setDuration(1200).start();
                        }
                    }
                });
            }
        }, 5000);

    }

    public void setItemOnclickListener(View.OnClickListener onclickListener) {
        itemOnclickListener = onclickListener;
    }

    public void destroy() {
        parentView.removeView(item);
        parentView = null;
        item = null;
    }
}